﻿if (!((window.DocumentTouch && document instanceof DocumentTouch) || 'ontouchstart' in window))
{
    var script = document.createElement("script");
    script.src = "js/touch.js";
    var tag = $("head").append(script);
    $.os.android = true;
    $.os.desktop = true;
    if (getCookie("savepass") != null)
        document.getElementById("css3").checked = true;
}
function Checklogin()
{
    if (queryString("unload") != null && queryString("unload") == "1")
    {
        window.history.forward(1);
        return;
    }
    else if (getCookie("savename") != null && getCookie("savename") != "" && getCookie("savepass") != null && getCookie("savepass") != "")
    {
        var name = getCookie("savename");
        var pass = getCookie("savepass");
        $('#username').val(name);
        $('#password').val(pass);
        //$.post('Proc/login.ashx', { uname: name, upass: pass }, function (data) { loginHome(data, name, pass,"1"); });
    }
}
function login()
{
    var name = $('#username').val();
    var pass = $('#password').val();
    if (name == "" || pass == "")
    {
        alert("【账号】或【密码】不能为空!");
    }
    else {
        $.post('Proc/login.ashx', { uname: name, upass: pass }, function (data) { loginHome(data,name,pass,"0"); });
    }
}

function $am(el) {
    el = typeof el == 'string' && el.indexOf("#") == -1 ? "#" + el : el;
    return jq(el).get(0);
}
function loginHome(data,u,p,s)
{
    if (data == "N")
        alert("账号或密码错误！");
    else if (data == "P")
        alert("账号已过期！");
    else if (data == "1")
    {
        if (document.getElementById("css3").checked)
        {
            if (getCookie("savename") == null)
            {
                SetCookie("savename", u, 7);
                SetCookie("savepass", p, 7);
            }
        }
        else
        {
            if (s == "0")
            {
                deleteCookie("savename");
                deleteCookie("savepass");
            }
        }
        window.location.href = "MobiApprove.htm";
    }
    else
        alert("未知错误，请确保网络畅通后登陆！");
}
function cancel()
{
    $('#username').val("");
    $('#password').val("");
}
function SetCookie(name,value,Days)//三个参数，一个是cookie的名子，一个是值,一个过期天数
{
    var exp = new Date();   
    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}
function getCookie(name)//取cookies函数        
{
    var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
    if (arr != null) return unescape(arr[2]); return null;
}
function deleteCookie(name)
{
    if (getCookie(name) == null)
        return;
    SetCookie(name, "", 0);
}
var queryString = function (key)
{
    return (document.location.search.match(new RegExp("(?:^\\?|&)" + key + "=(.*?)(?=&|$)")) || ['', null])[1];
}   